package com.yp.infrastructure.demo.domain.manager.impl;

import com.yp.infrastructure.demo.domain.manager.IPartnerManager;
import com.yp.infrastructure.demo.domain.mapper.PartnerMapper;
import com.yp.infrastructure.demo.domain.mapper.PartnerShipMapper;
import com.yp.infrastructure.demo.domain.mapper.PartnerTypeMapper;
import com.yp.infrastructure.demo.model.po.PartnerPo;
import com.yp.infrastructure.demo.model.po.PartnerShipPo;
import com.yp.infrastructure.demo.model.vo.PartnerVo;
import com.yp.infrastructure.common.annotation.LazyAutowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component
public class PartnerManagerImpl implements IPartnerManager {



    @LazyAutowired
    private PartnerMapper partnerMapper;

    @LazyAutowired
    private PartnerTypeMapper partnerTypeMapper;

    @LazyAutowired
    private PartnerShipMapper partnerShipMapper;

    @Override
    public List<PartnerVo> getPartnerList(String typeId, String owner) {
        List<PartnerShipPo> list = partnerShipMapper.getList(typeId, owner);
        List<PartnerVo> result = new ArrayList<>();
        if (null == list || list.isEmpty()) return result;
        list.forEach(x -> {
            PartnerPo po = partnerMapper.getInstance(x.getPartnerId());
            if (null != po) {
                PartnerVo vo = PartnerVo.from(PartnerVo.class, po);
                result.add(vo);
            }
        });
        return result;
    }

    @Override
    public void savePartner(String owner, String typeId, PartnerVo instance) {
        PartnerPo partner = PartnerPo.from(PartnerPo.class, instance);
        PartnerShipPo partnerShip = PartnerShipPo.builder()
                .ownerId(owner)
                .typeId(typeId)
                .partnerId(partner.getId())
                .build();
        partnerShipMapper.save(partnerShip);
        partnerMapper.save(partner);
    }
}
